package com.zx.framework.core.pagination;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zx.framework.core.util.StringUtil;
import com.zx.framework.core.util.gson.GsonUtil;

/**
 * 分页处理类,修改了部分参数名称
 * 
 * @author stylewolf 2013-10-11
 * 
 */
public class Pagination<E> {

	private static final Logger LOGGER = Logger.getLogger(Pagination.class);
	/**
	 * 每页显示记录数
	 */
	private int rows;
	/**
	 * 当前页码
	 */
	private int page;
	/**
	 * 总记录数
	 */
	private long total;
	/**
	 * 总页数
	 */
	private int pageCount;
	/**
	 * 模糊查询字符串
	 */
	private String fuzzy;
	/**
	 * 高级模式 1是 0否
	 */
	private String advance;
	/**
	 * 排序方式
	 */
	private String sort;

	/**
	 * 页面数据对象集合
	 */
	private List<E> datas = new ArrayList<E>();

	public Pagination() {
		super();
	}

	public Pagination(int total, int rows, int page, List<E> datas) {
		super();
		this.datas = datas;
		initPagination(total, rows, page);
	}

	/**
	 * 分页初始化
	 * 
	 * @param totalRecord
	 * @param pageSize
	 * @param current
	 */
	public void initPagination(int total, int rows, int page) {
		this.total = total;

		if (rows < 0) {
			rows = 10;
		}

		this.rows = rows;

		if (page <= 0 && total > 0) {
			page = 1;
		} else if (total == 0) {
			page = 0;
		}
		this.page = page;

		pageCount = total % rows;

		pageCount = pageCount == 0 ? total / rows : total / rows + 1;

		if (page > pageCount) {
			page = pageCount;
		}
		this.page = page;
		if (LOGGER.isDebugEnabled()) {
			LOGGER.debug("total:" + total + ",rows:" + rows + ",currentPage:"
					+ page + ",pageCount:" + pageCount);
		}
	}

	/**
	 * 初始化
	 * 
	 * @param total
	 * @param rows
	 * @param page
	 * @param sort
	 */
	public void initPagination(int total, int rows, int page, String sort) {
		initPagination(total, rows, page);
		setSort(sort);
	}

	/**
	 * 排序对象
	 * 
	 * @author stylewolf
	 * 
	 */
	public class Sort {
		private String property;

		private String direction;

		public Sort(String property, String direction) {
			super();
			this.property = property;
			this.direction = direction;
		}

		public String getProperty() {
			return property;
		}

		public void setProperty(String property) {
			this.property = property;
		}

		public String getDirection() {
			return direction;
		}

		public void setDirection(String direction) {
			this.direction = direction;
		}
	}

	/**
	 * 返回排序对象
	 * 
	 * @return
	 */
	public List<Sort> getSorts() {
		if (StringUtil.isEmpty(sort)) {
			return new ArrayList<Sort>();
		} else {
			Gson gson = GsonUtil.getGsonBuidler().create();
			Type type = new TypeToken<List<Sort>>() {
			}.getType();
			List<Sort> sorts = gson.fromJson(sort, type);
			return sorts;
		}
	}

	/**
	 * 
	 * @param sort
	 * @return
	 */
	public List<Sort> getSorts(String sort) {
		this.sort = sort;
		return getSorts();
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public long getTotal() {
		return total;
	}

	public void setTotal(long total) {
		this.total = total;
	}

	public int getPageCount() {
		return pageCount;
	}

	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}

	public String getFuzzy() {
		return fuzzy;
	}

	public void setFuzzy(String fuzzy) {
		this.fuzzy = fuzzy;
	}

	public String getAdvance() {
		return advance;
	}

	public void setAdvance(String advance) {
		this.advance = advance;
	}

	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public List<E> getDatas() {
		return datas;
	}

	public void setDatas(List<E> datas) {
		this.datas = datas;
	}

}
